var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store

Page({
  data: {
    ticketNumber:'', 
    deviceId:'', 
    deptId:'',
    operateArray:[
      {id:'',name:'无',value:0},
      {id:'checkSwitch',name:'检查开关'},
      {id:'requiresInterlock',name:'锁具联动'},
      {id:'tagNoClosing',name:'挂牌-禁止合闸'},
      {id:'tagGrounded',name:'挂牌-已接地'},
      {id:'tagWorkHere',name:'挂牌-在此工作'},
    ],
    operateAttent:'',
    operateAttentName:'无',
    operationType:[],
    operationTasksList:1,
    operationStatus:1,
    pageList: [], // 页面要展示的数据
    isShow: false,  
    titleType:'添加', // 当前选中的select [id] 
    taskDescription:'',
    desNum:'',
    optIndex:0,
    operatorId:'',
    type:''
  },
  onLoad: function (options) {
    console.log(options)
    this.setData({
      deviceId: options.deviceId,
      ticketNumber: options.ticketNumber,
      deptId:options.deptId,
      type:options.type,
      operationTasksList:options.operationTasksList,
      operationStatus:options.operationStatus,
      operatorId:Number(options.operatorId),
    })  
    if(this.data.type=='edit'&&this.data.operationTasksList!=0){
      this._editOptLibrary()
    }else{
      api.$get(api.operateTicket.operationType, { 
        deviceId: this.data.deviceId}).then((res) => {
        this.setData({
          operationType: res.data
        })
        this._operationLibrary()
      })
    }
    
  },
  onShow: function () {
    
  },
//  工作票，操作票---开始
_operationLibrary(){
  api.$get(api.operateTicket.operationLibrary, { 
    deviceId: this.data.deviceId, 
    operationType: this.data.operationType[this.data.optIndex], 
    ticketNumber: this.data.ticketNumber}).then((res) => {
      this.setData({
        pageList: res.data||[]
      })
    })
},
_editOptLibrary(){
  api.$get(api.operateTicket.ticketOperationList, { 
    ticketNumber: this.data.ticketNumber}).then((res) => {
      this.setData({
        pageList: res.data||[]
      })
    })
},
selectOperationType(){
  var that=this
  wx.showActionSheet({
    itemList: that.data.operationType,
    success (res) {
      console.log(res)
      that.setData({
        "optIndex":res.tapIndex
      })
      that._operationLibrary()
    },
    fail (res) {
      console.log(res.errMsg)
    }
  })
},
deleteMeasures(e){
  const {type,item,idx}=$.detail(e)
  const pageList=this.data.pageList
  var that=this
  wx.showModal({
    title: '提示',
    content: '是否删除此步骤？',
    success (res) {
      if (res.confirm) {
        pageList.splice(idx,1)
        that.setData({pageList:pageList.map((item,index)=>{
            return {...item,
              taskOrder:index,
            }
          }) 
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
},
// 工作票，操作票---结束
showPopup(e) {
  const {type,item,idx}=$.detail(e)
  this.setData({ isShow: true});
  if(type=='修改'){
    this.setData({ show: true,
      titleType:type,
      taskDescription:item.taskDescription,
      desNum:idx+1,
      operateAttent:item.checkSwitch?'checkSwitch':item.requiresInterlock?'requiresInterlock':item.tagNoClosing?'tagNoClosing':item.tagGrounded?'tagGrounded':item.tagWorkHere?'tagWorkHere':'',
      operateAttentName:item.checkSwitch?'检查开关':item.requiresInterlock?'锁具联动':item.tagNoClosing?'挂牌-禁止合闸':item.tagGrounded?'挂牌-已接地':item.tagWorkHere?'挂牌-在此工作':'',
   });
  }else{
    this.setData({ show: true,
      titleType:type,
      taskDescription:'',
      operateAttent:'',
      operateAttentName:'无',
      desNum:this.data.pageList.length+1
   });
  }
  
},
bindPickerChange: function(e) {
  const operateArray=this.data.operateArray
  console.log('picker发送选择改变，携带值为', e.detail.value)
  this.setData({
    operateAttent: operateArray[e.detail.value].id,
    operateAttentName: operateArray[e.detail.value].name,
  })
},
onSure(){
  if(this.data.taskDescription==''||this.data.desNum==''){
    $.toast('请输入描述信息')
  }
  const pageList=this.data.pageList
  if(this.data.titleType=='添加'){
    pageList.splice(this.data.desNum-1, 0, {
      ...pageList[this.data.desNum-2],
      operationId:this.data.operatorId,
      taskDescription:this.data.taskDescription,
      [this.data.operateAttent]:1
    })
  }else{
    pageList.splice(this.data.desNum-1, 1, {
      ...pageList[this.data.desNum-1],
      taskDescription:this.data.taskDescription,
      operationId:this.data.operatorId,
      checkSwitch:0,
      requiresInterlock:0,
      tagNoClosing:0,
      tagGrounded:0,
      tagWorkHere:0,
      [this.data.operateAttent]:1
    })
    
  }
  this.setData({ isShow: false,
    pageList:pageList.map((item,index)=>{
      return {...item,taskOrder:index}
    }) 
  });
},
onClose() {
  this.setData({ isShow: false });
},
changeDesCont(e){
  console.log(e.detail.value)
  this.setData({taskDescription:e.detail.value})
},
changeDesNum(e){
  this.setData({desNum:e.detail.value})
},

  
toTicketRisk(e){
  if(this.data.type=='edit'&&this.data.operationStatus!=0){
    api.$post(api.operateTicket.operationTasksSave,{
      ticketNumber:this.data.ticketNumber,
      operationTasksList:this.data.pageList,
    }).then((res) => {
      if(res.code==200){
        api.$get(api.operateTicket.submitTicketWork,{ticketNumber:this.data.ticketNumber}).then((res3) => {
          $.linkurl(`/pages/operationTicket/operationIndex/operationIndex`,'back')
        })
      }
    })
    
  }else{
    api.$post(api.operateTicket.operationTasksSave,{
      ticketNumber:this.data.ticketNumber,
      operationTasksList:this.data.pageList,
    }).then((res) => {
      if(res.code==200){
        $.linkurl(`/pages/operationTicket/riskManage/riskManage?ticketNumber=${this.data.ticketNumber}`,'redirect')
      }
    })
  }
  
},



})